//
//  MyShopDemandTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopDemandTabVC.h"
#import "HomeModel.h"
#import "MyShopDemandCell.h"
#import "WriteRequirementTitleCell.h"
@interface MyShopDemandTabVC ()<MyShopDemandCellDelegate>
@property(nonatomic,strong) NSMutableArray *onlineDemandArray;
@property(nonatomic,strong) NSMutableArray *offlineDemandArray;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,strong) NSDictionary  *dict;
@property(nonatomic,strong) HomeModel *downMode;
@property(nonatomic,strong) HomeModel *upModel;
@property(nonatomic,strong) NSIndexPath *indexPath;
@end
static NSString *identifierNeed = @"MyShopDemandCell";
static NSString *identifierTitle = @"titleCell";
@implementation MyShopDemandTabVC
-(NSMutableArray *)onlineDemandArray
{
    if (!_onlineDemandArray) {
        _onlineDemandArray = [NSMutableArray array];
        
    }
    return _onlineDemandArray;
}
-(NSMutableArray *)offlineDemandArray
{
    if (!_offlineDemandArray) {
        _offlineDemandArray = [NSMutableArray array];
        
    }
    return _offlineDemandArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.downMode = [[HomeModel alloc]init];
    self.upModel = [[HomeModel alloc]init];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyShopDemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierTitle];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectShelvesDemand:) name:@"selectShelvesDemand" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(delectDemand:) name:@"delectDemand" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downShelves:) name:@"downShelves" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onShelves:) name:@"onShelves" object:nil];
    [self reloadInternetData];
    
}
-(void)reloadInternetData {
    
    self.onlineDemandArray = nil;
    self.offlineDemandArray = nil;
//    NSString *pages = [NSString stringWithFormat:@"%ld",self.pageNum];
//    NSString *rows = [NSString stringWithFormat:@"%ld",self.pageSize];
//    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
//    [WSProgressHUD show];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:53 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)selectShelvesDemand:(NSNotification *)info
{
    
    NSDictionary *dict = info.userInfo;
    XLog(@"wode dianou dict:%@",dict);
    NSDictionary *date = [dict objectForKey:@"date"];
    NSArray *downDemand = [date objectForKey:@"downDemand"];
    NSArray *upDemand = [date objectForKey:@"upDemand"];
//    NSInteger empty = [EmptyModel stringIsNullOrEmpty:demandArray];
    if (upDemand.count) {
        for (NSDictionary *dict in upDemand) {
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            
            [self.onlineDemandArray addObject:model];
            
        }
    }
    if (downDemand.count) {
        for (NSDictionary *dict in downDemand) {
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            
            [self.offlineDemandArray addObject:model];
            
        }
    }
        [self.tableView reloadData];
        [WSProgressHUD dismiss];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.onlineDemandArray.count?self.onlineDemandArray.count+1:0;
    }else {
        return self.offlineDemandArray.count?self.offlineDemandArray.count+1:0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0) {
        WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierTitle forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        switch (indexPath.section) {
            case 0:
                cell.titleL.text = @"已上架需求";
                break;
            case 1:
                cell.titleL.text = @"未上架需求";
                break;
            default:
                break;
                
        }
        return cell;
    }else {
        MyShopDemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierNeed forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.indexPath = indexPath;
        switch (indexPath.section) {
            case 0:
            {
                if (self.onlineDemandArray.count) {
                    [cell.onOffLineBtn setTitle:@"下架" forState:UIControlStateNormal];
                    [cell.onOffLineBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
                    HomeModel *model = self.onlineDemandArray[indexPath.row-1];
                    cell.homeModel = model;
                }
            }
                break;
            case 1:
            {
                [cell.onOffLineBtn setTitle:@"上架" forState:UIControlStateNormal];
                [cell.onOffLineBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:152/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
                if (self.offlineDemandArray.count) {
                    HomeModel *model = self.offlineDemandArray[indexPath.row-1];
                    cell.homeModel = model;
                }
                
                
            }
                break;
            default:
                break;
        }
        
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (indexPath.row != 0) {
            HomeModel *homeMode = self.onlineDemandArray[indexPath.row-1];
            if (self.returnValueBlock) {
                self.returnValueBlock(homeMode);
            };
        }
    }else {
        if (indexPath.row != 0) {
            HomeModel *homeMode = self.offlineDemandArray[indexPath.row-1];
            if (self.returnValueBlock) {
                self.returnValueBlock(homeMode);
            };
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 60;
    }else {
        return 144;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 1)];
    return headerView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,1)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section != 0) {
        return 10;
    }else {
        return 0.1;
    }
}
#pragma mark-代理:删除,下架,修改,操作,联系...
- (void)myShopDemandCell:(MyShopDemandCell *)myShopDemandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
    if (indexPath.section == 0) {
        self.upModel = self.onlineDemandArray[indexPath.row -1];
        NSString *ids = [NSString stringWithFormat:@"%ld",self.upModel.id];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken],
                      @"id":ids,
                      };
        XLog(@"self.dict:%@",self.dict);
        XLog(@"clickBtn.tag:%ld",clickBtn.tag);
        switch (clickBtn.tag) {
            case 0://删除
            {
                [CustomeManager POSTWithURLIndex:54 parameters:self.dict success:^(NSDictionary *dict) {
                    
                } failure:^(NSError *error) {
                    
                }];
            }
                break;
            case 1://下架
            {
                [CustomeManager POSTWithURLIndex:55 parameters:self.dict success:^(NSDictionary *dict) {
                    
                } failure:^(NSError *error) {
                    
                }];
            }
                break;
            case 2:
            {
//                [CustomeManager POSTWithURLIndex:56 parameters:self.dict success:^(NSDictionary *dict) {
//
//                } failure:^(NSError *error) {
//
//                }];
            }
                break;
            case 3:
            {
//                [CustomeManager POSTWithURLIndex:56 parameters:self.dict success:^(NSDictionary *dict) {
//
//                } failure:^(NSError *error) {
//
//                }];
            }
                break;
            default:
            {
                XLog(@"clickBtn.selected:%d",clickBtn.selected);
                self.upModel.show = clickBtn.selected;
//                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
                break;
        }
       
    }else {
        self.downMode = self.offlineDemandArray[indexPath.row -1];
        NSString *ids = [NSString stringWithFormat:@"%ld",self.downMode.id];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken],
                      @"id":ids,
                      };
        XLog(@"self.dict:%@",self.dict);
        XLog(@"clickBtn.tag:%ld",clickBtn.tag);
        switch (clickBtn.tag) {//删除
            case 0:
            {
                [CustomeManager POSTWithURLIndex:54 parameters:self.dict success:^(NSDictionary *dict) {
                    
                } failure:^(NSError *error) {
                    
                }];
            }
                break;
            case 1:
            {
                [CustomeManager POSTWithURLIndex:56 parameters:self.dict success:^(NSDictionary *dict) {
                    
                } failure:^(NSError *error) {
                    
                }];
            }
                break;
            case 2:
            {
//                [CustomeManager POSTWithURLIndex:56 parameters:self.dict success:^(NSDictionary *dict) {
//
//                } failure:^(NSError *error) {
//
//                }];
            }
                break;
                
            default:
            {
                XLog(@"clickBtn.selected:%d",clickBtn.selected);
                self.downMode.show = clickBtn.selected;
//                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
                break;
        }
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark-代理:删除
-(void)delectDemand:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    [self reloadInternetData];
}
#pragma mark-代理:下架
-(void)downShelves:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"下架成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
        [self reloadInternetData];
    });
}
#pragma mark-代理:上架
-(void)onShelves:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"上架成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
    [self reloadInternetData];
    });
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    XLog(@"scrollView");
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
@end
